package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.api.user.UserApi;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.common.persistence.dao.MeetUserMapper;
import com.stylefeng.guns.rest.common.persistence.model.MeetUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: jiaru
 * @Description:
 * @Date: Created on 2019/3/16
 * @Modify:
 */
@Service(loadbalance = "roundrobin")
@Component
public class UserServiceImpl implements UserApi {

    @Autowired
    private MeetUserMapper meetUserMapper;

    /**
     * 用户登录业务
     * @param username  用户名
     * @param password  密码
     * @return
     */
    @Override
    public int login(String username, String password) {
        // 根据登陆账号获取数据库信息
        MeetUser user = new MeetUser();
        user.setUsername(username);

        MeetUser result = meetUserMapper.selectOne(user);

        // 获取到的结果，然后与加密以后的密码做匹配
        if(result!=null && result.getUuid()>0){
            String md5Password = MD5Util.encrypt(password);
            if(StringUtils.equals(md5Password, result.getPassword())){
                return result.getUuid();
            }
        }
        return 0;
    }

    /**
     * 用户注册
     * @param model     用户注册信息
     * @return
     */
    @Override
    public boolean register(UserModel model) {
        // 注册信息实体转换数据实体
        MeetUser user = new MeetUser();
        user.setUsername(model.getUsername());
        user.setEmail(model.getEmail());
        user.setAddress(model.getAddress());
        user.setPhone(model.getPhone());
        // 数据加密 【MD5混淆加密 + 盐值 -> Shiro加密】
        String md5Password = MD5Util.encrypt(model.getPassword());
        user.setPassword(md5Password); // 注意

        // 数据实体存入数据库
        Integer insert = meetUserMapper.insert(user);

        // 返回结果集
        return insert > 0;
    }

    /**
     * 检查用户名是否已存在
     * @param username      用户名
     * @return
     */
    @Override
    public boolean checkUsername(String username) {
        EntityWrapper<MeetUser> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("username", username);
        Integer result = meetUserMapper.selectCount(entityWrapper);
        return result == null || result <= 0;
    }

    /**
     * 拼装userInfoModel参数
     * @param user      用户信息
     * @return
     */
    private UserInfoModel do2UserInfo(MeetUser user) {
        UserInfoModel model = new UserInfoModel();

        model.setId(user.getUuid());
        model.setHeadAddress(user.getHeadUrl());
        model.setPhone(user.getPhone());
        model.setEmail(user.getEmail());
        model.setUsername(user.getUsername());
        model.setNickname(user.getNickname());
        model.setLifeState(user.getLifeState());
        model.setBirthday(user.getBirthday());
        model.setAddress(user.getAddress());
        model.setSex(user.getSex());
        model.setCreateTime(user.getCreateTime());
        model.setUpdateTime(user.getUpdateTime());
        model.setBiography(user.getBiography());

        return model;
    }
    
    /**
     * 获取用户信息
     * @param id    用户id
     * @return
     */
    @Override
    public UserInfoModel getUserInfo(int id) {
        // 根据主键查询用户信息 
        MeetUser user = meetUserMapper.selectById(id);
        // 将MeetUser转换UserInfoModel
        UserInfoModel userInfoModel = do2UserInfo(user);
        // 返回UserInfoModel
        return userInfoModel;
    }

    /**
     * 更新用户信息
     * @param model     更新资料
     * @return
     */
    @Override
    public UserInfoModel updateUserInfo(UserInfoModel model) {
        // 将传入的参数转换为DO 【MeetUser】
        MeetUser user = new MeetUser();
        user.setUuid(model.getId());
        user.setNickname(model.getNickname());
        user.setLifeState(model.getLifeState());
        user.setBirthday(model.getBirthday());
        user.setBiography(model.getBiography());
        user.setHeadUrl(model.getHeadAddress());
        user.setEmail(model.getEmail());
        user.setAddress(model.getAddress());
        user.setPhone(model.getPhone());
        user.setSex(model.getSex());

        // DO存入数据库
        Integer integer = meetUserMapper.updateById(user);
        if (integer > 0) {
            // 将数据从数据库中读取出来，将结果返回给前端
            return getUserInfo(user.getUuid());
        } else {
            return null;
        }
    }
}
